package com.zy.web.demo.duoxiancheng.demo32;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/9/9 17:13
 */
public class Service32C {

    private ReentrantLock lock = new ReentrantLock();

    public void method(){
        try {
            if ( lock.tryLock(3, TimeUnit.SECONDS) ){
                System.out.println(Thread.currentThread().getName()+"获得锁时间："+System.currentTimeMillis());
                Thread.sleep(5000);
            }else {
                System.out.println(Thread.currentThread().getName()+"没有获得锁");
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            if ( lock.isHeldByCurrentThread() ){
                lock.unlock();
            }
        }
    }

}
